package servlet;

import com.alibaba.fastjson.JSON;
import dao.CommunityDao;
import dao.impl.CommunityDaoImpl;
import pojo.entity.Community;
import util.JDBCDruidUtil;
import util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/communityYun")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        1.
        String action = req.getParameter("action");
        switch(action){
            case "communityList":
                //        2.获取数据
                String communityName = req.getParameter("communityName");
                String startTime = req.getParameter("startTime");
                String endTime = req.getParameter("endTime");
                String pageNum = req.getParameter("pageNum");
                String pageSize = req.getParameter("pageSize");
//        3.封装数据
                CommunityDao dao = new CommunityDaoImpl();
                Map<String,Object> map = new HashMap<>();
                map.put("communityName",communityName);
                map.put("startTime",startTime);
                map.put("endTime",endTime);
                map.put("pageNum",pageNum);
                map.put("pageSize",pageSize);
                List<Community> list = dao.selectAll(map);
//                System.out.println(list);
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("list",list);
                JDBCDruidUtil.close();
                resultMap.put("total",dao.selectCount(map));
                JDBCDruidUtil.close();
                resp.getWriter().write(JSON.toJSONString(Result.success(resultMap)));
            case "":
                break;
            default:resp.getWriter().write(JSON.toJSONString(Result.error("接口不存在")));
        }
//        4.

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
    
}
